package com.mobilekites.activities;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ViewGroup;

import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.mobilekites.MobileKitesApp;
import com.mobilekites.R;
import com.mobilekites.api.DfeApi;
import com.mobilekites.api.model.OnDataChangedListener;
import com.mobilekites.downloader.PhotoDownloader;
import com.mobilekites.fragments.PageFragmentHost;
import com.mobilekites.ga.GoogleAnalytics;
import com.mobilekites.ga.GoogleAnalytics.GoogleAnalyticsImpl;
import com.mobilekites.layout.ContentFrame;
import com.mobilekites.layout.CustomActionBar;
import com.mobilekites.layout.LayoutSwitcher;
import com.mobilekites.layout.LayoutSwitcher.RetryButtonListener;
import com.mobilekites.util.ErrorStrings;
import com.mobilekites.util.KitesPrefers;

public abstract class KitesActivity extends ActionBarActivity implements
		PageFragmentHost, RetryButtonListener, OnDataChangedListener,
		ErrorListener {
	protected static final String TAG = "KiteActivity";
	protected ViewGroup mDataView;
	private LayoutSwitcher mLayoutSwitcher;
	private GoogleAnalyticsImpl mGAImpl;
	boolean mStateSaved;
	protected CustomActionBar mActionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView();
		mGAImpl = new GoogleAnalyticsImpl(getApplicationContext());
	}

	protected void setContentView() {
		ContentFrame contentFrame = new ContentFrame(getApplicationContext());
		contentFrame.setDataLayout(getLayoutInflater(), getLayoutRes(),
				R.id.kites_content_layout);
		mDataView = contentFrame.getDataLayout();
		mLayoutSwitcher = new LayoutSwitcher(contentFrame,
				R.id.kites_content_layout, R.id.kites_error_layout,
				R.id.kites_loading_layout, this, LayoutSwitcher.DATA_MODE);
		setContentView(contentFrame);
	}

	@Override
	protected void onStart() {
		super.onStart();
		mStateSaved = false;
		mGAImpl.onStart(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		mStateSaved = false;
		mGAImpl.onStop(this);
	}

	@Override
	protected void onResumeFragments() {
		super.onResumeFragments();
		mStateSaved = false;
	}

	protected abstract int getLayoutRes();

	protected abstract void requestData();

	protected void switchToBlank() {
		if (mLayoutSwitcher != null)
			mLayoutSwitcher.switchToBlankMode();
	}

	protected void switchToData() {
		if (mLayoutSwitcher != null)
			mLayoutSwitcher.switchToDataMode();
	}

	protected void switchToError(String msg) {
		if (mLayoutSwitcher != null) {
			mLayoutSwitcher.switchToErrorMode(msg);
		}
	}

	protected void switchToLoading() {
		if (mLayoutSwitcher != null)
			mLayoutSwitcher.switchToLoadingDelayed(350);
	}

	@Override
	public void onErrorResponse(VolleyError volleyerror) {
		switchToError(ErrorStrings.get(this, volleyerror));
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onRetry() {
		refresh();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDataView = null;
		mLayoutSwitcher = null;
		mGAImpl.onDestroy();
	}

	public boolean canChangeFragmentManagerState() {
		return mStateSaved || this == null ? false : true;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mStateSaved = true;
	}

	@Override
	public void onDataChanged() {
		switchToData();
	}

	public void refresh() {
		requestData();
	}

	@Override
	public ImageLoader getImageLoader() {
		return MobileKitesApp.get().getImageLoader();
	}

	@Override
	public DfeApi getDfeApi() {
		return MobileKitesApp.get().getDfeApi();
	}

	@Override
	public PhotoDownloader getPhotoDownloader() {
		return MobileKitesApp.get().getPhotoDownloader();
	}

	@Override
	public void showErrorDialog(String s, String s1, boolean flag) {

	}

	public GoogleAnalytics getGoogleAnalytics() {
		return mGAImpl;
	}

	public KitesPrefers getKitesPrefers() {
		return MobileKitesApp.get().getKitesPrefers();
	}

	public boolean isStateSaved() {
		return mStateSaved;
	}
}
